package fr.inserm.umr915.vcfserver.bio;

import java.io.File;
import java.io.IOException;

import net.sf.picard.reference.IndexedFastaSequenceFile;

public class ReferenceSequence
	{
	private File fasta;
	private IndexedFastaSequenceFile indexedFastaSequenceFile;
	public ReferenceSequence(File fasta)
		{
		this.fasta=fasta;
		}
	
	public synchronized void close()
		{
		this.indexedFastaSequenceFile=null;
		}
	
	/**
	 * 
	 * @param chrom
	 * @param start0 0-based start position inclusive
	 * @param end0 0-based end position exclusive
	 * @return
	 * @throws IOException
	 */
	public synchronized byte[] getSequence(String chrom,int start0,int end0) throws IOException
		{
		if(this.indexedFastaSequenceFile==null)
			{
			this.indexedFastaSequenceFile=new IndexedFastaSequenceFile(this.fasta);
			}
		return this.indexedFastaSequenceFile.getSubsequenceAt(chrom,start0+1,end0).getBases();
		}
	public synchronized byte[] getSequence(Segment seg) throws IOException
		{
		return getSequence(seg.getChromosome(),seg.getChromStart(),seg.getChromEnd());
		}
	}
